const app = getApp()
var server = require('../../utils/server');
var common = require('../../utils/common');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showLoginPop: false,
    voucherEventId: '',
    detail: '',
    isOpen: false,
    isLogin: true,
    isDisableLog:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.data.voucherEventId = options.scene;
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.getStorage({
      key: 'sso',
      success: res => {
        this.getEventDetail();
      },
      fail: res => {
        this.getUserInfo();
      }
    })
  },
  //获取用户信息
  getUserInfo() {
    wx.login({
      success: resp => {
        wx.getUserInfo({
          success: res => {
            this.setData({
              showLoginPop: false
            })
            var code = resp.code,
              that = this,
              userParams = {
                "js_code": code,
                "encryptedData": res.encryptedData,
                "iv": res.iv,
                "userinfo": res.userInfo,
                "longitude": app.globalData.locateLongitude,
                "latitude": app.globalData.locateLatitude,
                "province": app.globalData.locateProvince,
                "city": app.globalData.locateCity,
                "scene": ''
              };
            server.login(common.basePath + 'miniprogram/login/', userParams, function(res) {
              if (res.data.errno == "0") {
                wx.setStorage({
                  key: "sso",
                  data: res.data.data.sso,
                  success: ret => {
                    app.globalData.isGetWxUserinfo = true;
                    if (JSON.stringify(res.data.data.member) != "{}") {
                      that.storageUserinfo(res.data.data);
                    }
                  }
                })
              } else {
                common.showErrorToast(res.data.errmsg);
              }
            })
          },
          fail: res => {
            wx.removeStorage({
              key: 'userInfo',
              success: function(res) {
              }
            })
            this.setData({
              showLoginPop: true,
              isLogin: true
            })
          }
        })
      },
      fail: res => {
        this.setData({
          showLoginPop: true,
          isLogin: true
        })
      }
    })
  },
  //授权用户信息
  login(e) {
    this.setData({
      isDisableLog: true
    })
    if (e.detail.errMsg == "getUserInfo:ok") {
      this.setData({
        showLoginPop: false,
        isLogin: false
      })
      wx.login({
        success: resp => {
          wx.getUserInfo({
            success: res => {
              var code = resp.code,
                that = this,
                userParams = {
                  "js_code": code,
                  "encryptedData": res.encryptedData,
                  "iv": res.iv,
                  "userinfo": res.userInfo,
                  "longitude": app.globalData.locateLongitude,
                  "latitude": app.globalData.locateLatitude,
                  "province": app.globalData.locateProvince,
                  "city": app.globalData.locateCity,
                  "scene": ''
                }
              server.login(common.basePath + 'miniprogram/login/', userParams, function(res) {
                if (res.data.errno == "0") {
                  wx.setStorage({
                    key: "sso",
                    data: res.data.data.sso,
                    success: ret => {
                      that.setData({
                        isDisableLog: false
                      })
                      app.globalData.isGetWxUserinfo = true;
                      that.getEventDetail();
                      if (JSON.stringify(res.data.data.member) != "{}") {
                        that.storageUserinfo(res.data.data)
                      }
                    }
                  })
                } else {
                  that.setData({
                    isDisableLog: false
                  })
                  common.showErrorToast(res.data.errmsg)
                }
              })
            }
          })
        }
      })
    } else {
      this.setData({
        isDisableLog: false
      })
      wx.showToast({
        icon: 'none',
        title: '需要授权用户信息使用元初到家！'
      })
    }
  },
  getEventDetail() {
    wx.stopPullDownRefresh();
    server.postJSON(common.basePath + 'voucher/team/', {
      "voucher_event_id": this.data.voucherEventId,
      "voucher_team_id": "",
      "type": ""
    }, res => {
      if (res.data.errno == '0') {
        var data = res.data.data;
        this.setData({
          detail: data.voucher,
          status: data.voucher.status,
          showLoginPop: false,
          isLogin: true
        })
        if (data.voucher_team_list.length > 0) {
          wx.redirectTo({
            url: 'activity_invite?eventid=' + this.data.voucherEventId + '&&teamid=' + data.voucher_team.voucher_team_id,
          })
        }
      } else {
        if (res.data.errno == '100012' || res.data.errno == '100013') {
          this.setData({
            showLoginPop: true,
            isLogin: false
          })
        } else {
          common.showErrorToast(res.data.errmsg);

        }
      }
    })
  },
  //本地存储用户信息
  storageUserinfo(data) {
    wx.setStorage({
      key: "userInfo",
      data: data
    })
  },
  openTeam() {
    this.setData({
      isOpen: true
    })
    server.postJSON(common.basePath + 'voucher/addteam/', {
      "voucher_event_id": this.data.voucherEventId
    }, resp => {
      this.setData({
        isOpen: false
      })
      if (resp.data.errno == '0') {
        this.data.teamid = resp.data.data;
        wx.navigateTo({
          url: 'activity_invite?eventid=' + this.data.voucherEventId + '&&teamid=' + this.data.teamid,
        })
      } else {
        common.showErrorToast(resp.data.errmsg);
      }
    })
  },
  onPullDownRefresh() {
    this.getEventDetail();
  },
  onShareAppMessage(options) {
    var shareObj = {
      path: '/activity/pages/activityIndex?scene=' + this.data.voucherEventId,
      title: this.data.detail.share_desc,
      imageUrl: this.data.detail.goods_pic_url,
      success: function(res) {},
      fail: function() {
        console.log("分享失败")
      }
    }
    return shareObj;
  },
  showAgreement() {
    wx.navigateTo({
      url: '../../pages/common/agreement',
    })
  },
  showLoginPop() {
    this.setData({
      showLoginPop: true
    })
  },
  hideLoginPop() {
    this.setData({
      showLoginPop: false,
      isLogin: false
    })
  }
})